package com.example.field.config;

import com.example.field.config.ErrorCode.ErrorCode;

public class Result<T> {
    private String code;
    private String msg;
    private T data;

    public String getCode(){
        return code;
    }
    public void setCode(String code){
        this.code=code;
    }

    public String getMsg(){
        return msg;
    }
    public void setMsg(String msg){
        this.msg=msg;
    }

    public T getData(){
        return data;
    }
    public void setData(T data){
        this.data=data;
    }

    public Result(){

    }
    public Result(T data){
        this.data=data;
    }

    //成功时不返回数据的情况
    public static Result success(){
        Result result=new Result<>();
        result.setCode("0");
        result.setMsg("成功");
        return result;
    }

    //成功时只返回自定义消息的情况
    public static Result success(String msg){
        Result result=new Result<>();
        result.setCode("0");
        result.setMsg(msg);
        return result;
    }

    //成功时返回数据的情况
    public static <T> Result<T> success(T data){
        Result<T> result=new Result<>(data);
        result.setCode("0");
        result.setMsg("成功");
        result.setData(data);
        return result;
    }


    //失败情况
    public static Result error(String code){
        Result result=new Result<>();
        result.setCode(code);
        result.setMsg(ErrorCode.getErrorMessage(code));
        return result;
    }

}
